package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.Provider;
import com.easondongh.business.mapper.ProviderMapper;
import com.easondongh.business.vo.ProviderQueryVo;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.mapper.ProviderMapper;
import com.easondongh.business.domain.Provider;
import com.easondongh.business.service.ProviderService;
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService{
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public PageResult<Provider> getByPage(BasePageQueryVo<ProviderQueryVo> queryCondition) {
        IPage<Provider> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<Provider> queryWrapper = new QueryWrapper<>();
        ProviderQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getProviderName()), Provider.COL_PROVIDER_NAME, queryParams.getProviderName());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getConnectionPerson()), Provider.COL_CONNECTION_PERSON, queryParams.getConnectionPerson());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getPhone()), Provider.COL_PHONE, queryParams.getPhone());
        }
        IPage<Provider> iPage = this.providerMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public boolean enableById(Integer id) {
        Provider provider = new Provider();
        provider.setId(id);
        provider.setEnable(Constants.ENABLE);
        return this.providerMapper.updateById(provider) == 1;
    }

    @Override
    public boolean disableById(Integer id) {
        Provider provider = new Provider();
        provider.setId(id);
        provider.setEnable(Constants.DISABLE);
        return this.providerMapper.updateById(provider) == 1;
    }

    @Override
    public List<Provider> getAllEnableProvider() {
        return this.providerMapper.getAllEnableProvider();
    }
}
